<?
// /ui/commandhandler/request/group|renew|restore/group/[groupname][/status/ok]

class News_Command_AsyncDownloader
{
    var $Controller;
    var $Yenc; 
    function News_Command_AsyncDownloader($controller)
	{
	    $this->Controller = $controller; 
	}
	
	function DownloadandProcess($groupname, $pageindex)
	{ 
        $filename  = "data/{$groupname}.xml";  
		
		
        if (file_exists($filename)) 
        {
		    $xml     = simplexml_load_file($filename);
		    $parent  = $xml->xpath("//list/article[@index='{$pageindex}']");
		    $type    = $parent[0]->type;
		    $fileid  = $parent[0]->id;
		    
		    if ($type=='rar')
		    {  
		        $rars  = $xml->xpath("//list/article[@index='{$pageindex}']/items/article"); 
		        $cap   = $rars[0]->subject;
		       
		        $pic = array();
			    $ids = explode ('-', $rars[0]->id);
		        $key = $ids[0]; 
		        
		        for ($r=0;$r < sizeof($rars);$r++)
		        {
		            echo $rars[$r]->subject . "\n";
		            $msg = sprintf ('Downloading RAR files %s of %s', $r + 1, sizeof($rars));
		            $pic[] = $this->Controller->GetArticle ($groupname, $rars[$r]->id, "", false, false, $msg);
		            echo "{$msg}\n";
		        }  
		        
		        echo "Processing RARS...\n";
		        
//		        RAR POST-PROCESSING
		        $name = NULL;
		        $new  = array();
		        foreach ($pic as $file)              # loop thru the downloaded files to
		        {                                    # locate the RAR file.
		            $arr = explode ('.', $file);
		            $ext = array_pop ($arr);
		            if (strtolower($ext) == 'rar')
		            {
		                $name = $file;
		                break;
		            }
		        }
		        
		        if ($name)
		        {
			        $pop = "data/perm/$groupname";   # create a parent dir to put all this in
			        if (!is_dir($pop)) mkdir ($pop); # then a sub-folder to hold whatever images 
			        $dir = "{$pop}/{$key}";          # are extracted from the downloaded archive. 
			        mkdir ($dir);                    # below this goes a temp folder to hold the 
			        $tmp = "{$dir}/temp";            # extracted files so they can be collated
			        mkdir ($tmp);                    # after extraction           

			        $new[] = $pop;
			        $new[] = $dir;
			        
			        if (is_dir($tmp))
			        {
			            $cmd = "cd \"{$tmp}\";unrar e \"/var/www/html/{$name}\"";
			            echo $cmd;                   # unrar the files to a temp folder 
			            passthru ($cmd);             # for analysis. once files are downloaded
			            $arr = scandir ($tmp);       # load them into an array for collation.
			            print_r($arr);
			            $jpeg = NULL;
			            $film = NULL;
			            foreach ($arr as $file)
			            {
			                if (!($file=='.'||$file=='..'))
			                {
			                    if (preg_match ('/\.(jpg|jpeg|png|gif|bmp)/i', $file))
			                    {
			                        $jpeg = "{$dir}/{$file}";
			                        rename ("{$tmp}/{$file}", $jpeg);
			                    }
			                    else if (preg_match ('/\.(m4v|flv|mpg|mpeg|mov|wmv|avi|mkv)/i', $file))
			                    {
			                        $tube = "data/tube/$groupname";  
			                        if (!is_dir($tube)) mkdir ($tube);
			                        $dir  = "{$tube}/{$key}";    
			                        if (!is_dir($dir)) mkdir ($dir);
			                        $film = "{$dir}/{$file}";
			                        rename ("{$tmp}/{$file}", $film);
			                        $new[] = $tube;
			                        $new[] = $dir;
			                        $this->Controller->Queue->Read($film, $fileid, $groupname, $cap);   
			                    }
			                }
			            }
			            echo "Deleting temp folders...\n";
			            passthru ("rm -rf \"{$tmp}\"");
		                foreach ($pic as $file)
		                { 
		                    unlink ($file);    
		                } 
		                foreach ($new as $dir)
		                { 
		                    if (sizeof(scandir($dir)) <= 2) rmdir ($dir);    
		                } 
		                echo "Removing RARS...\n";
			            if (sizeof ($arr) > 2)
			            {
			                if ($film)
			                {
			                    $this->Controller->Queue->Start(); 
			                }
			                else if ($jpeg)
			                {
			                    $lab = "data/tube/{$groupname}"; 
			                    $this->Controller->Queue->CreateIndex($lab, $key, $rars[0]->subject, $arr, 'RAR', $jpeg);
			                }
			                else
			                {
			                    echo "No usable content was found.\n";
			                }
			            }
			            else
			            {
			                echo "No files were extracted!\n";
			            }
			        }
			        else
			        {
			             echo "Could not create {$tmp}\n";
			        }
		            
		        }
		        echo "Done\n"; 
		        return;
		    }
//		    END RAR PROCESSING
        }		    
	}  
 
    
    
}



